#include "VerticalBox.h"
#include "RelativeBox.h"
#include "Button.h"
#include "Label.h"
#include "Progress.h"
#include "ScrollableBar.h"
#include "TextBox.h"
#include "Widget.h"
#include "UIRender.h"
#include "HorizentalBox.h"
VerticalBox::VerticalBox()
{

}

VerticalBox::~VerticalBox()
{

}

void VerticalBox::UpdateLayout()
{
	int curY = pos.y.val;
	for (int i = 0; i < childs.size(); i++)
	{
		childs[i].second->pos.x = pos.x.val;
		childs[i].second->pos.y = curY;
		curY += childs[i].second->size.y.val;
	}
}

Control* VerticalBox::CreateControl(std::string sControlClassName)
{
	if (sControlClassName == "Button")
	{
		return new Button();
	}
	else if (sControlClassName == "Label")
	{
		return new Label();
	}
	else if (sControlClassName == "Progress")
	{
		return new Progress();
	}
	else if (sControlClassName == "RelativeBox")
	{
		return new RelativeBox();
	}
	else if (sControlClassName == "VerticalBox")
	{
		return new VerticalBox();
	}
	else if (sControlClassName == "HorizentalBox")
	{
		return new HorizentalBox();
	}
	else if (sControlClassName == "ScrollableBar")
	{
		return new ScrollableBar;
	}
	else if (sControlClassName == "TextBox")
	{
		return new TextBox;
	}
	else if (sControlClassName == "Widget")
	{
		return new Widget;
	}
	return nullptr;
}

void VerticalBox::ParseXmlElement(TiXmlElement* ele)
{
	if (strcmp(ele->Value(), "VerticalBox") == 0)
	{
		TiXmlNode* ti_control_node = ele->FirstChild();
		TiXmlElement* ele = ti_control_node->ToElement();
		while (ele)
		{
			Control* ctrl = CreateControl(ele->Value());
			/*if (strcmp(ele->Value(), "Widget") == 0)
			{
				Widget* wi = (Widget*)ctrl;
				wi->ParseXmlElement(ele);
			}*/
			ctrl->DoParseXmlElement(ele);
			ctrl->rootWindow = this->rootWindow;
			std::string sCtrlName = "";
			TiXmlAttribute* ta = ele->FirstAttribute();
			while (ta)
			{
				ctrl->SetAttribute(ta->Name(), ta->Value());
				/*	if (strcmp(ta->Name(), "pos") == 0)
					{
						int x, y;
						sscanf_s(ta->Value(), "%d,%d", &x, &y);
						ctrl->pos.x = x;
						ctrl->pos.y = y;
					}
					else if (strcmp(ta->Name(), "size") == 0)
					{
						int w, h;
						sscanf_s(ta->Value(), "%d,%d", &w, &h);
						ctrl->size.x = w;
						ctrl->size.y = h;
					}
					else if (strcmp(ta->Name(), "name") == 0)
					{
						sCtrlName = ta->Value();
					}*/
					/*else if (strcmp(ta->Name(), "bkcolor") == 0 || strcmp(ta->Name(), "bkclr") == 0)
					{
						sCtrlName = ta->Value();
						int w, h;
						ctrl->size.x = w;
						ctrl->size.y = h;
					}
					else if (strcmp(ta->Name(), "hovercolor") == 0 || strcmp(ta->Name(), "hoverclr") == 0)
					{
						UITheme::
					}*/

				ta = ta->Next();
			}

			if (ctrl)
			{
				childs.push_back(std::make_pair(sCtrlName, ctrl));
			}
			ele = ele->NextSiblingElement();
		}
	}
}

bool VerticalBox::SetAttribute(const char* attrName, const char* attrVal)
{
	if (Control::SetAttribute(attrName, attrVal)) {
		return true;
	}
	if (strcmp(attrName, "bkcolor") == 0)
	{
		sClrBk = attrVal;
		/*int x, y,z;
		sscanf_s(attrVal, "%d,%d,%d", &x, &y,&z);
		this->clrBk.r = x;
		this->clrBk.g = y;
		this->clrBk.b = z;*/
		return true;
	}
	if (strcmp(attrName, "size") == 0)
	{
		int x, y;
		sscanf_s(attrVal, "%d,%d", &x, &y);
		size.x = x;
		size.y = y;
		return true;
	}
	return false;
}

void VerticalBox::Draw()
{
	UIRender::DrawEdgeRect(pos.x.val, pos.y.val, size.x.val, size.y.val, sClrBk);
	UpdateLayout();
	for (int i = 0; i < childs.size(); i++)
	{
		Control* cld = childs[i].second;
		cld->Draw();
	}
}

void VerticalBox::onMouseMove(int x, int y)
{
	for (int i = 0; i < childs.size(); i++)
	{
		Control* cld = childs[i].second;
		cld->onMouseMove(x, y);
	}
}

void VerticalBox::onMouseButtonDown(int button, int x, int y)
{
	for (int i = 0; i < childs.size(); i++)
	{
		Control* cld = childs[i].second;
		cld->onMouseButtonDown(button, x, y);
	}
}

void VerticalBox::onMouseButtonUp(int button, int x, int y)
{
	for (int i = 0; i < childs.size(); i++)
	{
		Control* cld = childs[i].second;
		cld->onMouseButtonUp(button, x, y);
	}
}

